package com.samp.mybatis;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class SampSqlSession {

    private static final String CONFIG_FILE = "mybatis/mybatis-config.xml";

    private static SqlSessionFactory sqlSessionFactory;

    private static SqlSessionFactory getSqlSessionFactory(){
        if( sqlSessionFactory == null ){
            InputStream is = SampSqlSession.class.getClassLoader().getResourceAsStream(CONFIG_FILE);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        }
        return sqlSessionFactory;
    }

    public static SqlSession getSqlSession(){
        return getSqlSessionFactory().openSession();
    }

    public static void main(String[] args) {
        SqlSession sqlSession = getSqlSession();
        System.out.println(sqlSession.getConfiguration().toString());
    }

}
